let App = getApp();
Page({
  data: {
    goods_list: [], // 商品列表
    order_total_num: 0,
    order_total_price: 0,
    switchChecked: false,
    switchStyle: 'text-decoration: line-through',
    switch2Checked: false,
    switch2Style: 'text-decoration: line-through',
    select_all: false,
    car_goods:[],
    checkbox_goodsid:0,
  },
  
  onLoad: function (options) {
  
  },
  onShow: function () {
    this.getCartList();
    this.setData({
      checkbox_goodsid:'',
      select_all:false,
    })
  },
  /** 购物车列表 */
  getCartList: function () {
    let that = this;
    App._get('cart/getlists', {}, function (result) {
      console.log(result);
      that.setData(result.data);
      that.setData({
        order_total_price:0
      })
      //提示下架的商品
      if (result.data.error_msg != '') {
        //App.showError(result.data.error_msg);
      }
    });
  },

  onCloseItem: function (e) {
    console.log(e);
    if (e.detail !='right'){
      return;
    }
    var that = this;
    var goods_sku_id = e.currentTarget.dataset.goods_sku_id;
    var goods_id = e.currentTarget.id;
    wx.showModal({
      title: "提示",
      content: "您确定要移除当前商品吗?",
      success: function (e) {
        e.confirm && App._post('cart/delete', {
          goods_id: goods_id,
          goods_sku_id: goods_sku_id
        }, function (result) {
          that.getCartList();
          //清空勾选项
          that.setData({
            checkbox_goodsid:'',
            select_all:false
          })
          console.log(that.data.checkbox_goodsid);
          //把刚刚删除按钮的消去
          that.SwipeCellList = that.selectAllComponents(".vscell");
          that.SwipeCellList.forEach(function (value, index, array) {
            that.SwipeCellList[index].close();
          });
        });
      }
    });
  },
  onplus: function (e) {
    let that = this;
    var index = e.currentTarget.dataset.id;
    var goodsSkuId = e.currentTarget.dataset.goods_sku_id;
    var goods = that.data.goods_list[index];
    //var order_total_price = that.data.order_total_price;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    App._post('cart/add', {
      goods_id: goods.goods_id,
      goods_num: 1,
      goods_sku_id: goodsSkuId,
    }, function (result) {
      goods.total_num++;
      that.setData({
        ['goods_list[' + index + ']']: goods,
        // order_total_price: that.mathadd(order_total_price, goods.goods_price)
      });
    });
  },
  mathadd: function (arg1, arg2) {
    return (Number(arg1) + Number(arg2)).toFixed(2);
  },
  onsub: function (e) {
    let that = this;
    var index = e.currentTarget.dataset.id;
    var goodsSkuId = e.currentTarget.dataset.goods_sku_id;
    var goods = that.data.goods_list[index];
    //order_total_price = that.data.order_total_price;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    App._post('cart/sub', {
      goods_id: goods.goods_id,
      goods_sku_id: goodsSkuId,
    }, function (result) {
      console.log(result);
      goods.total_num--;
      goods.total_num > 0 &&
        that.setData({
          ['goods_list[' + index + ']']: goods,
          //order_total_price: that.mathsub(order_total_price, goods.goods_price)
        });
    });
  },
  mathsub: function (arg1, arg2) {
    return (Number(arg1) - Number(arg2)).toFixed(2);
  },


  onSubmit: function (e) {
    if (this.data.checkbox_goodsid == 0){
      App.showError('请勾选商品');
      return;
    }
    wx.navigateTo({
      url: './checout?type=cart&ids='+this.data.checkbox_goodsid
    })
  },
  switchChange: function(e){
    console.log(e);
  },
  switch2Change: function(e){
    let that = this;
    if(e.detail.value === true){
      that.setData({
        switchChecked:true
      })
    }else{
      that.setData({
        switchChecked:false
      })
    }
    
  },
  selectall: function (e) {
    let that = this;
    console.log(that.data.goods_list);
    var arr = '0';
    console.log((!that.data.select_all));
    //that.data.car_goods.length是商品的个数
    that.data.switchChecked = (!that.data.select_all)
    that.setData({switchChecked: (!that.data.select_all) })
    for (let i = 0; i < that.data.goods_list.length; i++) {
      that.setData({["goods_list["+i+"].checked"]: (!that.data.select_all) })
    //循环给每个商品的checked赋值
     if (that.data.goods_list[i].checked == true) {
     // 全选获取选中的值 以字符串拼接的形式 拼成一个字符串
      arr = arr +","+ that.data.goods_list[i].goods_id;
     }
    }
    console.log("arr="+arr)
    //赋值
    that.setData({
     car_goods: that.data.goods_list,
     select_all: (!that.data.select_all),
     checkbox_goodsid: arr
    })
    that.countPrice();
    console.log("good_list=" +that.data.checkbox_goodsid)
    },
     
    // 单选
    checkboxChange: function (e) {
    var that =this;
    //这加个0 是我为了方便后台处理。
     var checkbox_goodsid = '0';
     //先将所有勾选清除
     for (let a = 0; a < that.data.goods_list.length; a++) {
        that.setData({["goods_list["+a+"].checked"]: false })
     }
     for (let i = 0; i < e.detail.value.length; i++) {
      checkbox_goodsid = checkbox_goodsid+ ','+e.detail.value[i]
      for (let a = 0; a < that.data.goods_list.length; a++) {
       // that.setData({["goods_list["+i+"].checked"]: (!that.data.select_all) })
      //循环给每个商品的checked赋值
       if (that.data.goods_list[a].goods_id == e.detail.value[i]) {
         that.setData({["goods_list["+a+"].checked"]: true })
       }
      }
    }
    that.setData({
      checkbox_goodsid: checkbox_goodsid//单个选中的值
    })
    console.log(e)
    console.log("checkbox_goodsid=" +that.data.checkbox_goodsid)
    console.log("list=" ,that.data.goods_list)
    that.countPrice();
    },
    //重新计算总价
    countPrice:function(){
      let that = this;
      var totalPrice = Number(0);
      for (let a = 0; a < that.data.goods_list.length; a++) {
        if (that.data.goods_list[a].checked ==true) {
          //统计 价格*数量总和
          totalPrice = totalPrice + Number(that.data.goods_list[a].goods_price)*Number(that.data.goods_list[a].total_num);
        }
        that.setData({order_total_price: totalPrice })
     }
    }
})